package org.dubbo.consumer;

import com.alibaba.dubbo.remoting.TimeoutException;
import com.alibaba.dubbo.rpc.RpcException;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.registry.integration.RegistryProtocol;
import com.alibaba.dubbo.rpc.Protocol;

import lif.dubbo.EchoService;

/**
 * Hello world!
 */
public class Api {

    @SuppressWarnings("rawtypes")
    public static void start() throws Exception {

        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("127.0.0.1:2181");
        registry.setProtocol("zookeeper");

        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("an");

        ReferenceConfig<EchoService> reference = new ReferenceConfig<EchoService>();
        reference.setRegistry(registry);
        reference.setInterface(EchoService.class);
        reference.setCheck(false);
        reference.setApplication(applicationConfig);
        reference.setTimeout(1000);
        //版本号要一样,要都没有配置
        reference.setVersion("1.0.0");
        EchoService xxxService = reference.get();
        try {
            System.out.println("sleep");
            int i = 5;
            while (i > 0) {
                Thread.sleep(1000);
                System.out.println("---->" +i);
                i--;
            }
            System.out.println("go on ....");

            System.out.println("consumer " + xxxService.echo("lif")); // 显示调用结果

        } catch (RpcException e) {
            System.out.println("error" + e.getCode() + "---" + e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println(" error is =>" + (e instanceof TimeoutException));
            e.printStackTrace();

        }


    }

    public static void main(String[] args) throws Exception {
        start();
        System.exit(1);
    }

}
